package leetcode_1100;

/**
 *@author 周杨 
 *RobotBoundedInCircle_1041 有一系列的直走 左转 右转的指令 问这些无限个指令可否让机器人循环
 *describe:只需要执行4次判断机器人是否回到原点即可
 *2019年5月13日 上午14:00
 */
public class RobotBoundedInCircle_1041 {
	public boolean isRobotBounded(String instructions) {
        StringBuilder sb = new StringBuilder(instructions.length()*4);
        for(int i=0;i<4;++i) sb.append(instructions);//走四次 看能否回到远点
        int x=0,y=0,direction = 0;//上下左右
        for(int i=0;i<sb.length();++i) {
        	char c = sb.charAt(i);
        	if(c == 'G') {
        		if(direction == 0) x++;
        		else if(direction == 1) x--;
        		else if(direction == 2) y--;
        		else y++;
        	}else if(c == 'L') {
        		if(direction == 0) direction = 2;
        		else if(direction == 1) direction = 3;
        		else if(direction == 2) direction = 1;
        		else direction = 0;
        	}else {
        		if(direction == 0) direction = 3;
        		else if(direction == 1) direction = 2;
        		else if(direction == 2) direction = 0;
        		else direction = 1;
        	}
        }
        return x==0&&y==0;   
    }
}
